<?php

namespace app\common\model\banxi\core;

use app\common\enums\RangeLevelEnum;
use app\common\model\banxi\say\SayCategory;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 上课记录
 */
class CoreSayVocabularySetting extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_core_say_vocabulary_setting';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'rangeLevelCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getRangeLevelCnAttr($value, $data)
    {
        $value = $value ?: ($data['rangeLevel'] ?? '');
        return (new RangeLevelEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    public function listBeginner()
    {
        return $this->where('rangeLevel', RangeLevelEnum::E1)->select();
    }

    public function getByRangeLevelAndName($rangeLevel, $name)
    {
        return $this->where([
            'rangeLevel' => $rangeLevel,
            'rangeName' => $name
        ])->find();
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function cateGory()
    {
        return $this->hasOne(SayCategory::class, 'id', 'sayCategoryId');
    }
}